package com.yu.crm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.crm.domain.pojo.CrmDepartmentMember;
import com.yu.crm.service.CrmDepartmentMemberService;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 部门成员表 前端控制器
 * </p>
 *
 * @author donnie-bin
 * @since 2019-06-10
 */
@Api(tags = "部门成员管理")
@RestController
@RequestMapping("/crm-department-member")
public class CrmDepartmentMemberController {

    @Autowired
    CrmDepartmentMemberService memberService;

    @ApiOperation(value = "详情保存或修改")
    @PostMapping(value = "/save-or-update")
    public void saveOrUpdate(@RequestBody @Valid CrmDepartmentMember member, BindingResult bindingResult){
        ValidatorHelpers.validateParams(bindingResult);
        if (DataHelpers.isNotEmpty(member.getId())){
            ValidatorHelpers.notEmpty(member.getUserId(), "用户id不能为空");
        }
        memberService.saveOrUpdate(member);
    }

    @ApiOperation(value = "根据ID删除")
    @DeleteMapping(value = "/remove-by-id/{id}")
    public void removeById(@PathVariable Long id){
        memberService.removeById(id);
    }

    @ApiOperation(value = "详情查询")
    @GetMapping(value = "/get-by-id/{id}")
    public CrmDepartmentMember getById(@PathVariable Long id){
        return memberService.getById(id);
    }

    @ApiOperation(value = "分页列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "页面大小", required = false, dataType = "int")
    })
    @GetMapping(value = "/list")
    public IPage<CrmDepartmentMember> list(@ApiIgnore Page<CrmDepartmentMember> page, CrmDepartmentMember queryInfo){
        return memberService.list(page, queryInfo);
    }
}

